package paleGoldenRod.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ScroogleGUI implements EntryPoint {

	private final class GetCallback implements AsyncCallback {
		int instance;
		public GetCallback(int length) {
			instance=length-1;
		}

		public void onFailure(Throwable caught) {
		      // TODO Auto-generated method stub
		      
		 }

		public void onSuccess(Object result) {
		      String suggestions = (String) result;
				//String printedSuggestions = new String();
				label[0][0].setText("Received: " + suggestions);
		 }
	}

	
	private final class SendCallback implements AsyncCallback {
		int instance;
		public SendCallback(int length) {
			instance=length-1;
		}

		public void onFailure(Throwable caught) {
		      // TODO Auto-generated method stub
		      
		 }

		public void onSuccess(Object result) {
		      //String suggestions = (String) result;
				//String printedSuggestions = new String();
				label[0][0].setText("Success!");
		 }
	}
	/**
	 * This is the entry point method.
	 */
    private MessagingServiceAsync getMessagingService(){
        MessagingServiceAsync predictionService = (MessagingServiceAsync) 
		             GWT.create(MessagingService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) predictionService;
        String moduleRelativeUrl = GWT.getModuleBaseURL() + "messagingService";
        endpoint.setServiceEntryPoint(moduleRelativeUrl);
        return predictionService;
   }
	private TextBox textbox = new TextBox();
	private Label label[][] = new Label[3][5];
	public void onModuleLoad() {
        MessagingServiceAsync predictionService = this.getMessagingService();
        AsyncCallback callback = new AsyncCallback(){
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
			public void onSuccess(Object result) {
				int i = ((Integer)result).intValue();
				if(i>=0)
					new ScroogleGame(RootPanel.get(), i%2);
				else
					RootPanel.get("slot00").add(new HTML("Test"));
			}
        };
        predictionService.getPlayerID(callback);
	}
	
	protected void update() {
		// TODO Auto-generated method stub
        MessagingServiceAsync predictionService = this.getMessagingService();
        AsyncCallback callback = new GetCallback(0);
        predictionService.getString(callback);
	}
	protected void commit() {
		// TODO Auto-generated method stub
        MessagingServiceAsync predictionService = this.getMessagingService();
        AsyncCallback callback = new SendCallback(0);
        predictionService.sendString(textbox.getText(), callback);
	}
}
